<?php

/**
 * events actions.
 *
 * @package    campus-plateform
 * @subpackage events
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class eventsActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->events = Doctrine_Core::getTable('event')
      ->createQuery('a')
      ->execute();
  }

  public function executeSlider(sfWebRequest $request)
  {
    $this->events = Doctrine_Core::getTable('event')->getLastFiveEvents();

    $this->posts = Doctrine_Core::getTable('post')->getLastPosts();
  }

  public function executeInscription(sfWebRequest $request)
  {
      $Eventid = $request->getParameter('idEvent');      

      $UserHasEvent = new UserEvent();
      $UserHasEvent->setEventsId($Eventid);
      $UserHasEvent->setUsersId($Userid);
      $UserHasEvent->save();
      
      $this->redirect('events/slider');
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->event = Doctrine_Core::getTable('event')->find(array($request->getParameter('id')));

    $this->EventUsers = $this->event->getUsersHasEvents();
    $this->forward404Unless($this->event);
  }

  public function executeNew(sfWebRequest $request)
  {
    $jourd = $request->getParameter('jd');
    $moisd = $request->getParameter('md');
    $yeard = $request->getParameter('yd');

    $jourf = $request->getParameter('jf');
    $moisf = $request->getParameter('mf');
    $yearf = $request->getParameter('yf');

    $Date_debut = $moisd.'/'.$jourd.'/'.$yeard ;
    $Date_fin = $moisf.'/'.$jourf.'/'.$yearf ;

    $event = new Event();
    $event->setStartDate($Date_debut);
    $event->setEndDate($Date_fin);
    $this->form = new eventForm($event);
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new eventForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($event = Doctrine_Core::getTable('event')->find(array($request->getParameter('id'))), sprintf('Object event does not exist (%s).', $request->getParameter('id')));

    // Drag and drop
       if($request->getParameter('nbrdaymoved') != null)
       {

        $Dd = $animation->getDdebut();
        $Df = $animation->getDfin();
        $nbrdaymoved = $request->getParameter('nbrdaymoved');
        $stamp_dd = strtotime($Dd);
        $stamp_df = strtotime($Df);

        $stamp_dateFinal_dd = $stamp_dd + (24 * 60 * 60 * $nbrdaymoved);
        $stamp_dateFinal_df = $stamp_df + (24 * 60 * 60 * $nbrdaymoved);

        $DateFinal_dd = date("Y-m-d", $stamp_dateFinal_dd);
        $DateFinal_df = date("Y-m-d", $stamp_dateFinal_df);

        $event->setStartDate($DateFinal_dd);
        $animation->setEndDate($DateFinal_df);

        $event->save();
        $this->redirect('events/index');
       }// resize
       else if($request->getParameter('HDF') != null)
       {
           $HeureDeFin = $request->getParameter('HDF');
           $event->setStartDate($HeureDeFin);
           $event->save();
           $this->redirect('events/index');
       }
       else
       {
            $this->form = new eventForm($event);
       }
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($event = Doctrine_Core::getTable('event')->find(array($request->getParameter('id'))), sprintf('Object event does not exist (%s).', $request->getParameter('id')));
    $this->form = new eventForm($event);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($event = Doctrine_Core::getTable('event')->find(array($request->getParameter('id'))), sprintf('Object event does not exist (%s).', $request->getParameter('id')));
    $event->delete();

    $this->redirect('events/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $event = $form->save();

      $this->redirect('events/edit?id='.$event->getId());
    }
  }
}
